// component/topup/topup.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isnavigator:{
      type:Boolean,
      value:false
    },
    iszdy:{
      type:Boolean,
      value:false
    },
    datalist:{
      type:Array,
      value:'' 
    },
    datamoney:{
      type:String,
      value:0
    },
    amoneyindex:{
      type:String,
      value:0
    }
  },
  
  /**
   * 组件的初始数据
   */
  data: {
    topupamount : [
      {value: '1', name: '200',msg:'1'},
      {value: '2', name: '500',msg:'2'},
      {value: '3', name: '800',msg:'3'},
      {value: '4', name: '1500',msg:'4'},
      {value: '5', name: '2000',msg:'5'},
    ],
    list:[],
    price:null, //钱数
    okpic:0,
    changinputs:0
  },



  
  /**
   * 组件的方法列表
   */
  methods: {
/*钱币列表*/
    moneybutton(e) {
      let that = this;
      let index = e.currentTarget.dataset.index;
      let value = e.currentTarget.dataset.value;
      let datalist = this.properties.datalist;

      for(let i = 0; i<datalist.length; i++){
        datalist[i].select = false;
      }
      datalist[index].select = !datalist[index].select;
      let price=datalist[index].Price;
      console.log(price);
      that.setData({
        okpic:price,
        datalist
      })
    },
    /*关闭*/
    isTrue(e){
      let that = this;
      that.setData({
        isnavigator:false
      })
    },
    /*关闭自定义金额*/
    isTruezdy(e){
      let that = this;
      that.setData({
        iszdy:false
      })
    },
    changinputs(e){
      let that = this;
      console.log(e)
      that.setData({
        changinputs:e.detail.value
      })
    },
    //自定义微信支付
    okmoneys(e){
      let that = this
      let token =  wx.getStorageSync('token')
      wx.request({
        url: 'https://www.yulonging.com/dwweb/wxapi.ashx?cmd=uniOrder',
        data: {
         //请求后台的分页数据
        //  cmd:'uniOrder',
         token:token,
         orderno:'',
         totalFee:that.data.changinputs,
         officeId:1,
         IsCorp:that.properties.amoneyindex //根据切换充值
        },
        method: "get",
        success(res) {
     //微信支付调用
     wx.requestPayment
     (
       {
         "timeStamp": res.data.DATA.timeStamp,
         "nonceStr": res.data.DATA.nonceStr,
         "package": "prepay_id="+res.data.DATA.prepay_id,
         "signType": "MD5",
         "paySign": res.data.DATA.paySign,
         "success":function(res){
          wx.showToast({
            title: '支付成功',
            icon: 'success',
            duration: 1000
          })
           that.setData({
            isnavigator:false,
            iszdy:false
          })
         },
         "fail":function(res){
          wx.showToast({
            title: res,
            icon: 'error',
            duration: 1000
          })
         },
         "complete":function(res){
          wx.showToast({
            title: res,
            icon: 'error',
            duration: 1000
          })
         }
       }
     )
        }
      })
    },
    ononeself:function(e){
      let _this = this
      _this.setData({
        iszdy:true
      })
      console.log(2);
    },
    onmoney(e){
      let _this = this
      _this.setData({
        iszdy:false,
      })
    },
      //调用微信支付充值
  okmoney(e){
    let that = this
    let token =  wx.getStorageSync('token')
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx?cmd=uniOrder',
      data: {
       //请求后台的分页数据
      //  cmd:'uniOrder',
       token:token,
       orderno:'',
       totalFee:that.data.okpic,
       officeId:1,
       IsCorp:0
      },
      method: "get",
      success(res) {
          console.log(res)
          //微信支付调用
          wx.requestPayment
          (
            {
              "timeStamp": res.data.DATA.timeStamp,
              "nonceStr": res.data.DATA.nonceStr,
              "package": "prepay_id="+res.data.DATA.prepay_id,
              "signType": "MD5",
              "paySign": res.data.DATA.paySign,
              "success":function(res){
                console.log(222)
                that.setData({
                  isnavigator:false,
                  iszdy:false
                }) 
              },
              "fail":function(res){
                console.log(333)
              },
              "complete":function(res){
                console.log(444)
              }
            }
          )


      }
    })
  }
  },
  onload(options){
       //获得充值金额 
       wx.request({
        url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
        data: {
          token: token,
          cmd: 'getChargeMoneys',
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success (data) {
            if(data.data.STATUS=='Success'){
              that.setData({
                list: data.data.DATA,
              })
            }else{
              wx.showToast({
                title:data.data.DESC,
                icon: 'error',
                duration: 2000
              })
            }
        }
      })
  },


})
